package com.example.demo.common.com.example.demo;

import cn.hutool.core.math.MathUtil;
import cn.hutool.core.util.NumberUtil;
import cn.hutool.core.util.RandomUtil;
import com.alibaba.fastjson.JSONObject;
import com.amazonaws.services.dynamodbv2.xspec.B;
import com.amazonaws.services.dynamodbv2.xspec.S;
import org.apache.commons.lang3.StringUtils;
import org.apache.logging.log4j.util.Strings;

import java.io.FileOutputStream;
import java.math.BigDecimal;
import java.math.MathContext;
import java.math.RoundingMode;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;

public class Demo05 {

    private static List<String> list;

    static {
        list = new ArrayList<>();
        list.add("1");
        list.add("2");
        list.add("3");
        list.add("4");
        list.add("5");
        list.add("6");
        list.add("7");
        list.add("8");
        list.add("9");
        list.add("10");
    }

    public static void main(String[] args) {


        /*String priceFloat = "-10~10";
        String[] floatArray = StringUtils.split(priceFloat, "~");
        int min = Integer.parseInt(floatArray[0]);
        int max = Integer.parseInt(floatArray[1]);

        int c11 = RandomUtil.randomInt(min, max);

        System.out.println(c11);

        BigDecimal d1 = new BigDecimal(100 + c11);
        BigDecimal d2 = new BigDecimal(100);
        // 此处可控范围内浮动无需优化
        BigDecimal d3 = d1.divide(d2);

        BigDecimal d4 = new BigDecimal(90);
        System.out.println(NumberUtil.round(d4.multiply(d3), 2));


        System.out.println(JSONObject.parseArray("", Object.class));

        System.out.println(JSONObject.toJSONString(""));

        System.out.println(RandomUtil.randomInt(0, 0));

        String str1 = "[\"021000000\",\"021014000\",\"021014019\"]";
        List<String> ssda = JSONObject.parseArray(str1, String.class);
        System.out.println(JSONObject.toJSONString(ssda));

        String attrString = "[{\"attrName\":\"主要销售区域\",\"attrValue\":\"中国大陆\",\"random\":false},{\"attrName\":\"型号\",\"attrValue\":\"\",\"random\":true},{\"attrName\":\"加工定制\",\"attrValue\":\"否\",\"random\":false},{\"attrName\":\"有可授权的自有品牌\",\"attrValue\":\"无\",\"random\":false},{\"attrName\":\"商品品类\",\"attrValue\":\"通用\",\"random\":false},{\"attrName\":\"产品类型\",\"attrValue\":\"精品国货\",\"random\":false},{\"attrName\":\"适用肤质\",\"attrValue\":\"任何肤质\",\"random\":false},{\"attrName\":\"产地\",\"attrValue\":\"广州\",\"random\":false},{\"attrName\":\"生产许可证号\",\"attrValue\":\"粤妆20190058\",\"random\":false},{\"attrName\":\"化妆品功效\",\"attrValue\":\"补水保湿\",\"random\":false},{\"attrName\":\"特殊用途化妆品\",\"attrValue\":\"否\",\"random\":false},{\"attrName\":\"是否跨境出口专供货源\",\"attrValue\":\"否\",\"random\":false},{\"attrName\":\"规格\",\"attrValue\":\"正常规格\",\"random\":false},{\"attrName\":\"是否进口\",\"attrValue\":\"否\",\"random\":false},{\"attrName\":\"保质期\",\"attrValue\":\"三年\",\"random\":false}]" + "[{\"attrName\":\"品牌\",\"attrValue\":\"付付付\",\"random\":false},{\"attrName\":\"产地\",\"attrValue\":\"中国\",\"random\":false},{\"attrName\":\"订货号\",\"attrValue\":\"\",\"random\":true},{\"attrName\":\"订货\",\"attrValue\":\"\",\"random\":true},{\"attrName\":\"是否跨境出口专供货源\",\"attrValue\":\"否\",\"random\":false}]";
        String[] attrElemArray = StringUtils.substringsBetween(attrString, "attrValue\":\"", "\",");
        List<String> attrElemList = Arrays.asList(attrElemArray);
        System.out.println(JSONObject.toJSONString(attrElemList));

        List<String> sssss = Arrays.asList("111", "222", "333", "444", "555", "666");
        System.out.println(appendStringSpecifiedLength("张三1122334455", sssss, 20));*/

        System.out.println(substringSpecifiedLength("福福福福福付包装制品配附件家电部件电子外壳回收镀金线路板你好呀", 60));

        String str = "  mm 25包";
        //System.out.println(str.length());
        System.out.println(getWordCount(str));
        str = "张三";
        System.out.println(str.length());
        System.out.println(getWordCount(str));
        str = "Abd藏12";
        System.out.println(str.length());
        System.out.println(getWordCount(str));


        System.out.println();

        List<String> ad = Arrays.asList("1");

        int i = 0;

        int j = i > ad.size() ? RandomUtil.randomInt(ad.size()) : i;
        System.out.println(j);

        i = 1;

        j = i > ad.size() ? RandomUtil.randomInt(ad.size()) : i;
        System.out.println(j);
        i = 2;

        j = i > ad.size() ? RandomUtil.randomInt(ad.size()) : i;
        System.out.println(j);

        i = 3;
        j = i > ad.size() ? RandomUtil.randomInt(ad.size()) : i;
        System.out.println(j);

        i = 4;
        j = i > ad.size() ? RandomUtil.randomInt(ad.size()) : i;
        System.out.println(j);


        int x = RandomUtil.randomInt(-10, 10);
        BigDecimal ss = new BigDecimal(100 + x);
        BigDecimal xx = new BigDecimal(100);
        ss = ss.divide(xx);


        BigDecimal sss = new BigDecimal("0.4");

        System.out.println(ss);
        System.out.println(sss.multiply(ss).doubleValue());
        System.out.println(NumberUtil.round(sss.multiply(ss), 0));


        System.out.println(10 * (100 - 3) / 100);


        System.out.println(MathUtil.combinationCount(10, 5));

        System.out.println(MathUtil.arrangementCount(10, 5));


        List<String[]> r = MathUtil.combinationSelect(list.toArray(new String[10]), 5);
        System.out.println(r.size());

        //System.out.println(JSONObject.toJSONString(r));

        List<String> aList = Arrays.asList("A1", "A2", "A3", "A4");
        List<String> bList = Arrays.asList("B1", "B2");
        List<String> cList = Arrays.asList("C1", "C2", "C3");
        List<String> dList = Arrays.asList("D1", "D2", "D3", "D4");
        List<String> eList = Arrays.asList("E1", "E2", "E3", "E4", "E5");


        List<String> list = new ArrayList<>();
        StringBuilder sb = new StringBuilder();

        for (String s : aList) {
            for (String s1 : bList) {
                for (String s2 : cList) {
                    for (String s3 : dList) {
                        for (String s4 : eList) {
                            sb.setLength(0);
                            String item = sb.append(s).append(s1).append(s2).append(s3).append(s4).toString();
                            list.add(item);
                            if (list.size() >100) {
                                return;
                            }
                        }
                    }
                }
            }
        }

        aList.forEach(a -> bList.forEach(b -> cList.forEach(c -> dList.forEach(d -> eList.forEach(e -> {
            sb.setLength(0); // 重置 StringBuilder 的内容
            String item = sb.append(a).append(b).append(c).append(d).append(e).toString();
            list.add(item);
        })))));

        System.out.println(list.size());

        int totalIterations = aList.size() * bList.size() * cList.size() * dList.size() * eList.size();
        System.out.println(totalIterations);


    }


    /**
     * 追加字符串到指定长度
     *
     * @param originStr    源字符串
     * @param elements     元素池
     * @param specifiedLen 指定长度
     * @return 最终字符串
     */
    public static String appendStringSpecifiedLength(String originStr, List<String> elements, int specifiedLen) {
        if (Objects.isNull(originStr)) {
            return originStr;
        }

        for (String element : elements) {
            if (StringUtils.isBlank(element)) {
                continue;
            }
            originStr = String.format("%s%s", originStr, element);
            if (getWordCount(originStr) > specifiedLen) {
                break;
            }
        }
        return originStr;
    }

    public static int getWordCount(String str) {
        int length = 0;
        for (int i = 0; i < str.length(); i++) {
            int ascii = Character.codePointAt(str, i);
            if (ascii >= 0 && ascii <= 255) {
                if (ascii != 32) {
                    length++;
                }
            } else {
                length += 2;
            }
        }
        return length;
    }


    public static String substringSpecifiedLength(String originStr, int length) {
        if (getWordCount(originStr) <= length) {
            return originStr;
        }

        int len = 0;
        String result = Strings.EMPTY;
        for (int i = 0; i <= originStr.length(); i++) {
            int ascii = Character.codePointAt(originStr, i);
            if (ascii >= 0 && ascii <= 255) {
                len++;
            } else {
                len += 2;
            }
            if (len >= length) {
                result = substring(originStr, 0, i + 1);
                break;
            }
        }

        return result;
    }

    public static String substring(final String str, int start, int end) {
        if (str == null) {
            return null;
        }

        if (end < 0) {
            end = str.length() + end;
        }
        if (start < 0) {
            start = str.length() + start;
        }

        if (end > str.length()) {
            end = str.length();
        }

        if (start > end) {
            return null;
        }

        if (start < 0) {
            start = 0;
        }
        if (end < 0) {
            end = 0;
        }

        return str.substring(start, end);
    }
}
